/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.uff.es2.xadrez.gui;

import br.uff.es2.xadrez.core.Bishop;
import br.uff.es2.xadrez.core.Board;
import br.uff.es2.xadrez.core.Knight;
import br.uff.es2.xadrez.core.Piece;
import br.uff.es2.xadrez.core.Queen;
import br.uff.es2.xadrez.core.Rook;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author user
 */
public class PromocaoPecas extends JFrame implements ActionListener {

    //componentes de tela
    private JButton botao1;
    private JButton botao2;
    private JButton botao3;
    private JButton botao4;
    private ImageIcon torre;
    private ImageIcon rainha;
    private ImageIcon bispo;
    private ImageIcon cavalo;
    //fim dos componentes de tela
    public static int[] novaPosicao;
    private JLabel promocao;

    public PromocaoPecas() {
        torre = new ImageIcon("img\\torre-branca.png");
        botao1 = new JButton();
        botao1.setSize(600, 200);
        botao1.setIcon(torre);
        botao1.setVisible(true);
        botao1.addActionListener(this);

        rainha = new ImageIcon("img\\rainha-branca.png");
        setLayout(new FlowLayout());
        botao2 = new JButton();
        botao2.setSize(600, 200);
        botao2.setIcon(rainha);
        botao2.setVisible(true);
        botao2.addActionListener(this);

        bispo = new ImageIcon("img\\bispo-branco.png");
        setLayout(new FlowLayout());
        botao3 = new JButton();
        botao3.setSize(600, 200);
        botao3.setIcon(bispo);
        botao3.setVisible(true);
        botao3.addActionListener(this);

        cavalo = new ImageIcon("img\\cavalo-branco.png");
        setLayout(new FlowLayout());
        botao4 = new JButton();
        botao4.setSize(600, 200);
        botao4.setIcon(cavalo);
        botao4.setVisible(true);
        botao4.addActionListener(this);

        add(botao1);
        add(botao2);
        add(botao3);
        add(botao4);
    }

    @SuppressWarnings("static-access")
    public void actionPerformed(ActionEvent e) {
        JPanel panel = (JPanel) Tabuleiro.chessBoard.getComponent(novaPosicao[1]);
        panel.removeAll();
        if(e.getSource() == botao1){
            Board.board[novaPosicao[0]][novaPosicao[1]] = new Rook(Piece.Color.WHITE, "img\\torre-branca.png");
            JLabel pieceLabel = new JLabel(new ImageIcon("img\\torre-branca.png"));
            panel.add(pieceLabel);
        }else if (e.getSource() == botao2) {
            Board.board[novaPosicao[0]][novaPosicao[1]] = new Queen(Piece.Color.WHITE, "img\\rainha-branca.png");
            JLabel pieceLabel = new JLabel(new ImageIcon("img\\rainha-branca.png"));
//                    JPanel panel = Tabuleiro.chessBoard.getComponent(line * 8 + column);
            panel.add(pieceLabel);
        } else if (e.getSource() == botao3) {
            Board.board[novaPosicao[0]][novaPosicao[1]] = new Bishop(Piece.Color.WHITE, "img\\bispo-branco.png");
            JLabel pieceLabel = new JLabel(new ImageIcon("img\\bispo-branco.png"));
            panel.add(pieceLabel);
        } else if (e.getSource() == botao4) {
            Board.board[novaPosicao[0]][novaPosicao[1]] = new Knight(Piece.Color.WHITE, "img\\cavalo-branco.png");
            JLabel pieceLabel = new JLabel(new ImageIcon("img\\cavalo-branco.png"));
            panel.add(pieceLabel);
        }
        panel.setVisible(true);
        Tabuleiro.chessBoard.validate();
        dispose();
//        Board testeBoard = new Board();
//        testeBoard.board = Board.board;
//        testeBoard.setTurn(Piece.Color.BLACK);
//        try {
//            testeBoard.move(novaPosicao, novaPosicao);
//        } catch (WrongMoveException ex) {
//            Logger.getLogger(PromocaoPecas.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (RoqueException ex) {
//            Logger.getLogger(PromocaoPecas.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (CheckMateException ex) {
//            Logger.getLogger(PromocaoPecas.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (PromocaoDePecasException ex) {
//            Logger.getLogger(PromocaoPecas.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    /**
     * @return the novaPosicao
     */
    public int[] getNovaPosicao() {
        return novaPosicao;
    }

    /**
     * @param novaPosicao the novaPosicao to set
     */
    public void setNovaPosicao(int[] novaPosicao) {
        this.novaPosicao = novaPosicao;
    }

    /**
     * @return the promocao
     */
    public JLabel getPromocao() {
        return promocao;
    }

    /**
     * @param promocao the promocao to set
     */
    public void setPromocao(JLabel promocao) {
        this.promocao = promocao;
    }

//    public static void main(String[] args){
//        JFrame p = new PromocaoPecas();
//        p.setTitle("Promoção de peças");
//        p.setVisible(true);
//        p.setSize(500, 200);
//        p.setLocationRelativeTo(null);
//        p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//    }
}
